package com.antifly.common.config;

import com.antifly.common.service.RedisService;
import com.antifly.common.service.impl.RedisServiceImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.io.Serializable;

/**
 * RedisHelper自动注入
 *
 * @author LuoJiaHui
 * @date 2023/4/25 10:07
 * @description
 */
@ConditionalOnWebApplication
@Configuration
@ComponentScan(basePackages = {"com.antifly.common.config", "com.antifly.common.service"})
public class CustomRedisAutoConfiguration {

    @Bean
    public RedisTemplate<Serializable, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Serializable, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        // 定义key序列化方式
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        // 定义value序列化方式
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        return redisTemplate;
    }

    @Bean(name = "redisHelper")
    public RedisService redisService() {
        return new RedisServiceImpl();
    }
}
